from django.http import JsonResponse
from good_article.models import GoodCategory, GoodArticle
from django.views.decorators.csrf import csrf_exempt, csrf_protect

from good_article.models import GoodArticle
from good_article.serializers import GoodArticleSerializer
from post.models import Post
from good_article.other import check_token


def good_article(request):
    '''
    获取识货文章列表
    :param request:
    :return:
    '''
    good_article = GoodArticle.objects.all()
    serializer = GoodArticleSerializer(good_article, many=True)
    data = {
        'code': 0,
        'msg': 'ok',
        'data': serializer.data
    }
    return JsonResponse(data, safe=False)

@csrf_exempt
def update_article(request, user_id, content_id):
    print(4)
    # token = request.GET.get('token', None)

    if request.method == 'POST':
        token = request.POST.get('token', None)
        print(2)
        # t = check_token(token, user_id)
        if check_token(token, user_id):
            print(1)
            # title_update = request.POST.get('title',None)
            content_update = request.POST.get('content',None)
            print(content_update)
            if content_update:
                content = Post.objects.filter(author=user_id).filter(id=content_id).update(content=content_update)
            else:
                content = Post.objects.filter(author=user_id).filter(id=content_id).first().content

            code = 0
            msg = '请求成功'
            data1 = [
                {
                    'content': content
                }
            ]
        else:
            print(5)
            code = '10000'
            msg = '请求失败'
            data1 = []
    else:
        print(3)
        code = '10000'
        msg = '请求失败'
        data1 = []
    data = {
        'code': code,
        'msg': msg,
        'data': data1,
    }
    return JsonResponse(data, safe=False)


#获取所有识货分类的信息
def good_categories(request):
    big_data={}
    Goods_list=[]
    data={}
    pp=1
    while True:
        GC = GoodCategory.objects.get(id=pp)
        data={
           'name':GC.name,
           'id':GC.id,
            'ordered':GC.ordered
       }
        Goods_list.append(data)
        pp+=1
        if GoodCategory.objects.filter(id=pp):
            continue
        else:break
    big_data={
        'code':10000,
        'msg':'ok',
        'good_categories':Goods_list
    }
    big_data=json.dumps(big_data)
    return JsonResponse(big_data,safe=False)

#获取指定识货分类的文章列表
def good_articles_id(request,id):
    # good_id = GoodCategory.objects.get(pk=id).id
    good = GoodArticle.objects.filter(category__id=id).values('id','title','content','author__name','image','add_time','click_count','praise_count','collect_count')
    good_list=list(good)

    biglist=[]
    for i in good_list:

        data={
            'id':i['id'],
            'title':i['title'],
            'content':i['content'],
            'author__name':i['author__name'],
            'image':i['image']
        }
        biglist.append(data)
    bigdata={
        'code':10000,
        'msg':'ok',
        'good_articles':biglist
    }

    return JsonResponse(bigdata, safe=False)

#获取具体的文章内容
def articles(request,pid):
    GG=GoodArticle.objects.get(id=pid)
    author=GG.author.name
    data={
        'id':GG.id,
        'title':GG.title,
        'author':author,
        'conent':GG.content,
    }
    uu=json.dumps(data)

    return JsonResponse(uu,safe=False)

#删除识货文章
def del_good_article(request,cid):
    essay=GoodArticle.objects.get(id=cid)
    essay.is_delete=True
    essay.save()
    data={
        'code': 0,
        'msg': 'ok',
        'data': {
            'title':essay.title,
            '状态':'删除成功'
    }
    }
    # data=json.dumps(data)
    return JsonResponse(data,safe=False)